home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / programming / other / scm / slib / comlist.scm < prev    next >
Text File  |  1999-04-19  |  11KB  |  351 lines

  1. ;;"comlist.scm" Implementation of COMMON LISP list functions for Scheme
  2. ; Copyright (C) 1991, 1993, 1995 Aubrey Jaffer.
  3. ;
  4. ;Permission to copy this software, to redistribute it, and to use it
  5. ;for any purpose is granted, subject to the following restrictions and
  6. ;understandings.
  7. ;
  8. ;1.  Any copy made of this software must include this copyright notice
  9. ;in full.
  10. ;
  11. ;2.  I have made no warrantee or representation that the operation of
  12. ;this software will be error-free, and I am under no obligation to
  13. ;provide any services, by way of maintenance, update, or otherwise.
  14. ;
  15. ;3.  In conjunction with products arising from the use of this
  16. ;material, there shall be no use of my name in any advertising,
  17. ;promotional, or sales literature without prior written consent in
  18. ;each case.
  19.  
  20. ;;; Some of these functions may be already defined in your Scheme.
  21. ;;; Comment out those definitions for functions which are already defined.
  22.  
  23. ;;;; LIST FUNCTIONS FROM COMMON LISP
  24.  
  25. ;;;From: hugh@ear.mit.edu (Hugh Secker-Walker)
  26. (define (comlist:make-list k . init)
  27.   (set! init (if (pair? init) (car init)))
  28.   (do ((k k (+ -1 k))
  29.        (result '() (cons init result)))
  30.       ((<= k 0) result)))
  31.  
  32. (define (comlist:copy-list lst) (append lst '()))
  33.  
  34. (define (comlist:adjoin e l) (if (memv e l) l (cons e l)))
  35.  
  36. (define (comlist:union l1 l2)
  37.   (cond ((null? l1) l2)
  38.     ((null? l2) l1)
  39.     (else (comlist:union (cdr l1) (comlist:adjoin (car l1) l2)))))
  40.  
  41. (define (comlist:intersection l1 l2)
  42.   (cond ((null? l1) l1)
  43.     ((null? l2) l2)
  44.     ((memv (car l1) l2) (cons (car l1) (comlist:intersection (cdr l1) l2)))
  45.     (else (comlist:intersection (cdr l1) l2))))
  46.  
  47. (define (comlist:set-difference l1 l2)
  48.   (cond ((null? l1) l1)
  49.     ((memv (car l1) l2) (comlist:set-difference (cdr l1) l2))
  50.     (else (cons (car l1) (comlist:set-difference (cdr l1) l2)))))
  51.  
  52. (define (comlist:position obj lst)
  53.   (letrec ((pos (lambda (n lst)
  54.           (cond ((null? lst) #f)
  55.             ((eqv? obj (car lst)) n)
  56.             (else (pos (+ 1 n) (cdr lst)))))))
  57.     (pos 0 lst)))
  58.  
  59. (define (comlist:reduce-init p init l)
  60.   (if (null? l)
  61.       init
  62.       (comlist:reduce-init p (p init (car l)) (cdr l))))
  63.  
  64. (define (comlist:reduce p l)
  65.   (cond ((null? l) l)
  66.     ((null? (cdr l)) (car l))
  67.     (else (comlist:reduce-init p (car l) (cdr l)))))
  68.  
  69. (define (comlist:some pred l . rest)
  70.   (cond ((null? rest)
  71.      (let mapf ((l l))
  72.        (and (not (null? l))
  73.         (or (pred (car l)) (mapf (cdr l))))))
  74.     (else (let mapf ((l l) (rest rest))
  75.         (and (not (null? l))
  76.              (or (apply pred (car l) (map car rest))
  77.              (mapf (cdr l) (map cdr rest))))))))
  78.  
  79. (define (comlist:every pred l . rest)
  80.   (cond ((null? rest)
  81.      (let mapf ((l l))
  82.        (or (null? l)
  83.            (and (pred (car l)) (mapf (cdr l))))))
  84.     (else (let mapf ((l l) (rest rest))
  85.         (or (null? l)
  86.             (and (apply pred (car l) (map car rest))
  87.              (mapf (cdr l) (map cdr rest))))))))
  88.  
  89. (define (comlist:notany pred . ls) (not (apply comlist:some pred ls)))
  90.  
  91. (define (comlist:notevery pred . ls) (not (apply comlist:every pred ls)))
  92.  
  93. (define (comlist:find-if t l)
  94.   (cond ((null? l) #f)
  95.     ((t (car l)) (car l))
  96.     (else (comlist:find-if t (cdr l)))))
  97.  
  98. (define (comlist:member-if t l)
  99.   (cond ((null? l) #f)
  100.     ((t (car l)) l)
  101.     (else (comlist:member-if t (cdr l)))))
  102.  
  103. (define (comlist:remove p l)
  104.   (cond ((null? l) l)
  105.     ((eqv? p (car l)) (comlist:remove p (cdr l)))
  106.     (else (cons (car l) (comlist:remove p (cdr l))))))
  107.  
  108. (define (comlist:remove-if p l)
  109.   (cond ((null? l) l)
  110.     ((p (car l)) (comlist:remove-if p (cdr l)))
  111.     (else (cons (car l) (comlist:remove-if p (cdr l))))))
  112.  
  113. (define (comlist:remove-if-not p l)
  114.   (cond ((null? l) l)
  115.     ((p (car l)) (cons (car l) (comlist:remove-if-not p (cdr l))))
  116.     (else (comlist:remove-if-not p (cdr l)))))
  117.  
  118. (define comlist:nconc
  119.   (if (provided? 'rev2-procedures) append!
  120.       (lambda args
  121.     (cond ((null? args) '())
  122.           ((null? (cdr args)) (car args))
  123.           ((null? (car args)) (apply comlist:nconc (cdr args)))
  124.           (else
  125.            (set-cdr! (last-pair (car args))
  126.              (apply comlist:nconc (cdr args)))
  127.            (car args))))))
  128.  
  129. ;;;From: hugh@ear.mit.edu (Hugh Secker-Walker)
  130. (define (comlist:nreverse rev-it)
  131. ;;; Reverse order of elements of LIST by mutating cdrs.
  132.   (cond ((null? rev-it) rev-it)
  133.     ((not (list? rev-it))
  134.      (slib:error "nreverse: Not a list in arg1" rev-it))
  135.     (else (do ((reved '() rev-it)
  136.            (rev-cdr (cdr rev-it) (cdr rev-cdr))
  137.            (rev-it rev-it rev-cdr))
  138.           ((begin (set-cdr! rev-it reved) (null? rev-cdr)) rev-it)))))
  139.  
  140. (define (comlist:last lst n)
  141.   (comlist:nthcdr (- (length lst) n) lst))
  142.  
  143. (define (comlist:butlast lst n)
  144.   (letrec ((l (- (length lst) n))
  145.        (bl (lambda (lst n)
  146.          (cond ((null? lst) lst)
  147.                ((positive? n)
  148.             (cons (car lst) (bl (cdr lst) (+ -1 n))))
  149.                (else '())))))
  150.     (bl lst (if (negative? n)
  151.         (slib:error "negative argument to butlast" n)
  152.         l))))
  153.  
  154. (define (comlist:nthcdr n lst)
  155.   (if (zero? n) lst (comlist:nthcdr (+ -1 n) (cdr lst))))
  156.  
  157. (define (comlist:butnthcdr n lst)
  158.   (letrec ((bn (lambda (lst n)
  159.          (cond ((null? lst) lst)
  160.                ((positive? n)
  161.             (cons (car lst) (bn (cdr lst) (+ -1 n))))
  162.                (else '())))))
  163.     (bn lst (if (negative? n)
  164.         (slib:error "negative argument to butnthcdr" n)
  165.         n))))
  166.  
  167. ;;;; CONDITIONALS
  168.  
  169. (define (comlist:and? . args)
  170.   (cond ((null? args) #t)
  171.     ((car args) (apply comlist:and? (cdr args)))
  172.     (else #f)))
  173.  
  174. (define (comlist:or? . args)
  175.   (cond ((null? args) #f)
  176.     ((car args) #t)
  177.     (else (apply comlist:or? (cdr args)))))
  178.  
  179. ;;; Checks to see if a list has any duplicate MEMBERs.
  180. (define (comlist:has-duplicates? lst)
  181.   (cond ((null? lst) #f)
  182.     ((member (car lst) (cdr lst)) #t)
  183.     (else (comlist:has-duplicates? (cdr lst)))))
  184.  
  185. ;;; remove duplicates of MEMBERs of a list
  186. (define (comlist:remove-duplicates lst)
  187.   (letrec ((rem-dup
  188.         (lambda (lst nlst)
  189.           (cond ((null? lst) nlst)
  190.             ((member (car lst) nlst) (rem-dup (cdr lst) nlst))
  191.             (else (rem-dup (cdr lst) (cons (car lst) nlst)))))))
  192.     (rem-dup lst '())))
  193.  
  194. (define (comlist:list* x . y)    
  195.   (define (list*1 x)
  196.     (if (null? (cdr x))
  197.     (car x)
  198.     (cons (car x) (list*1 (cdr x)))))
  199.   (if (null? y)
  200.       x
  201.       (cons x (list*1 y))))
  202.  
  203. (define (comlist:atom? a)
  204.   (not (pair? a)))
  205.  
  206. (define (type-of obj)
  207.   (cond
  208.    ((null? obj)        'null)
  209.    ((boolean? obj)    'boolean)
  210.    ((char? obj)        'char)
  211.    ((number? obj)    'number)
  212.    ((string? obj)    'string)
  213.    ((symbol? obj)    'symbol)
  214.    ((input-port? obj)    'port)
  215.    ((output-port? obj)    'port)
  216.    ((procedure? obj)    'procedure)
  217.    ((eof-object? obj)    'eof-object)
  218.    ((list? obj)        'list)
  219.    ((pair? obj)        'pair)
  220.    ((and (provided? 'array) (array? obj))    'array)
  221.    ((and (provided? 'record) (record? obj))    'record)
  222.    ((vector? obj)    'vector)
  223.    (else        '?)))
  224.  
  225. (define (coerce obj result-type)
  226.   (define (err) (slib:error 'coerce "couldn't" obj '-> result-type))
  227.   (define obj-type (type-of obj))
  228.   (cond
  229.    ((eq? obj-type result-type) obj)
  230.    (else
  231.     (case obj-type
  232.       ((char)   (case result-type
  233.           ((number integer) (char->integer obj))
  234.           ((string) (string obj))
  235.           ((symbol) (string->symbol (string obj)))
  236.           ((list)   (list obj))
  237.           ((vector) (vector obj))
  238.           (else     (err))))
  239.       ((number) (case result-type
  240.           ((char)   (integer->char obj))
  241.           ((atom)   obj)
  242.           ((integer) obj)
  243.           ((string) (number->string obj))
  244.           ((symbol) (string->symbol (number->string obj)))
  245.           ((list)   (string->list (number->string obj)))
  246.           ((vector) (list->vector (string->list (number->string obj))))
  247.           (else     (err))))
  248.       ((string) (case result-type
  249.           ((char)   (if (= 1 (string-length obj)) (string-ref obj 0)
  250.                 (err)))
  251.           ((atom)   (or (string->number obj) (string->symbol obj)))
  252.           ((number integer) (or (string->number obj) (err)))
  253.           ((symbol) (string->symbol obj))
  254.           ((list)   (string->list obj))
  255.           ((vector) (list->vector (string->list obj)))
  256.           (else     (err))))
  257.       ((symbol) (case result-type
  258.           ((char)   (coerce (symbol->string obj) 'char))
  259.           ((number integer) (coerce (symbol->string obj) 'number))
  260.           ((string) (symbol->string obj))
  261.           ((atom)   obj)
  262.           ((list)   (string->list (symbol->string obj)))
  263.           ((vector) (list->vector (string->list (symbol->string obj))))
  264.           (else     (err))))
  265.       ((list)   (case result-type
  266.           ((char)   (if (and (= 1 (length obj))
  267.                      (char? (car obj)))
  268.                 (car obj)
  269.                 (err)))
  270.           ((number integer)
  271.            (or (string->number (list->string obj)) (err)))
  272.           ((string) (list->string obj))
  273.           ((symbol) (string->symbol (list->string obj)))
  274.           ((vector) (list->vector obj))
  275.           (else     (err))))
  276.       ((vector) (case result-type
  277.           ((char)   (if (and (= 1 (vector-length obj))
  278.                      (char? (vector-ref obj 0)))
  279.                 (vector-ref obj 0)
  280.                 (err)))
  281.           ((number integer)
  282.            (or (string->number (coerce obj string)) (err)))
  283.           ((string) (list->string (vector->list obj)))
  284.           ((symbol) (string->symbol (coerce obj string)))
  285.           ((list)   (list->vector obj))
  286.           (else     (err))))
  287.       (else (err))))))
  288.  
  289. (define (comlist:delete obj list)
  290.   (let delete ((list list))
  291.     (cond ((null? list) '())
  292.       ((equal? obj (car list)) (delete (cdr list)))
  293.       (else
  294.        (set-cdr! list (delete (cdr list)))
  295.        list)))) 
  296.  
  297. (define (comlist:delete-if pred list)
  298.   (let delete-if ((list list))
  299.     (cond ((null? list) '())
  300.       ((pred (car list)) (delete-if (cdr list)))
  301.       (else
  302.        (set-cdr! list (delete-if (cdr list)))
  303.        list)))) 
  304.  
  305. (define (comlist:delete-if-not pred list)
  306.   (let delete-if ((list list))
  307.     (cond ((null? list) '())
  308.       ((not (pred (car list))) (delete-if (cdr list)))
  309.       (else
  310.        (set-cdr! list (delete-if (cdr list)))
  311.        list)))) 
  312.  
  313. ;;; exports
  314.  
  315. (define make-list comlist:make-list)
  316. (define copy-list comlist:copy-list)
  317. (define adjoin comlist:adjoin)
  318. (define union comlist:union)
  319. (define intersection comlist:intersection)
  320. (define set-difference comlist:set-difference)
  321. (define position comlist:position)
  322. (define reduce-init comlist:reduce-init)
  323. (define reduce comlist:reduce) ; reduce is also in collect.scm
  324. (define some comlist:some)
  325. (define every comlist:every)
  326. (define notevery comlist:notevery)
  327. (define notany comlist:notany)
  328. (define find-if comlist:find-if)
  329. (define member-if comlist:member-if)
  330. (define remove comlist:remove)
  331. (define remove-if comlist:remove-if)
  332. (define remove-if-not comlist:remove-if-not)
  333. (define nconc comlist:nconc)
  334. (define nreverse comlist:nreverse)
  335. (define last comlist:last)
  336. (define butlast comlist:butlast)
  337. (define nthcdr comlist:nthcdr)
  338. (define butnthcdr comlist:butnthcdr)
  339. (define and? comlist:and?)
  340. (define or? comlist:or?)
  341. (define has-duplicates? comlist:has-duplicates?)
  342. (define remove-duplicates comlist:remove-duplicates)
  343.  
  344. (define delete-if-not comlist:delete-if-not)
  345. (define delete-if comlist:delete-if)
  346. (define delete comlist:delete)
  347. (define comlist:atom comlist:atom?)
  348. (define atom comlist:atom?)
  349. (define atom? comlist:atom?)
  350. (define list* comlist:list*)
  351.